package com.usian.day04.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetAddress;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2023/8/2 9:46
 */
@Component
@Slf4j
public class BrushProofFilter implements GlobalFilter,Ordered {


    @Autowired
    StringRedisTemplate stringRedisTemplate;



    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {


        //通过Redis来记录请求访问次数，每次访问都进行递减，如果次数小于0就返回错误信息，
        //当到了指定的失效。redis 会对这个key删除

        //获取IP
        InetAddress address = exchange.getRequest().getRemoteAddress().getAddress();
        log.info("ip:{}",address);

        String url = exchange.getRequest().getPath().toString();
        log.info("urL:{}",url);


        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
